package cn.tedu.store.controller;

import cn.tedu.store.entity.CartVO;
import cn.tedu.store.entity.UserEntity;
import cn.tedu.store.service.ICartService;
import cn.tedu.store.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/carts")
public class CartController {

    @Autowired
    ICartService service;

    @GetMapping("/findByCids")
    public JsonResult<List<CartVO>> findByCids(HttpServletResponse resp,Integer[] cids, Integer uid,HttpSession session){
//        UserEntity user=(UserEntity)session.getAttribute("user");
//        List<CartVO> data=service.findByCids(cids,user.getUid());
        resp.setHeader("Access-Control-Allow-Origin", "*");
        List<CartVO> data=service.findByCids(cids,uid);
        for (CartVO c : data){
            System.out.println(c + "----------------------");
        }

        return JsonResult.getSuccessJR(data);
    }

    @PostMapping("/findByCid")
    public JsonResult<List<CartVO>> findByCid(Integer[] cids, Integer uid){
//        UserEntity user=(UserEntity)session.getAttribute("user");
//        List<CartVO> data=service.findByCids(cids,user.getUid());
        List<CartVO> data=service.findByCids(cids,uid);
        return JsonResult.getSuccessJR(data);
    }

    @PostMapping("/changeNum")
    public JsonResult<Void> changeNum( HttpServletResponse resp,Integer cid,Integer uid,String username, Integer num,HttpSession session){
//        UserEntity user=(UserEntity)session.getAttribute("user");
        resp.setHeader("Access-Control-Allow-Origin", "*");
        service.changeNum(cid,num,uid,username);
        return JsonResult.getSuccessJR();
    }

    @PostMapping("/removeList")
    public JsonResult<Void> removeCartList( HttpServletResponse resp,Integer[] cids){
        resp.setHeader("Access-Control-Allow-Origin", "*");
        service.removeCartList(cids);
        return JsonResult.getSuccessJR();
    }

    @PostMapping("/remove")
    public JsonResult<Void> removeCart(HttpServletResponse resp,Integer cid,Integer uid,HttpSession session){
//        UserEntity user=(UserEntity)session.getAttribute("user");
        resp.setHeader("Access-Control-Allow-Origin", "*");
        service.removeCart(cid,uid);
        return JsonResult.getSuccessJR();
    }

    @GetMapping("/list")
    public JsonResult<List<CartVO>> getCartList(HttpServletResponse resp, Integer uid, HttpSession session){
//        UserEntity user=(UserEntity)session.getAttribute("user");
        resp.setHeader("Access-Control-Allow-Origin", "*");
        List<CartVO> list=service.findCartList(uid);
        return JsonResult.getSuccessJR(list);
    }

    @PostMapping("/create")
    public JsonResult<Void> createCart(HttpServletResponse resp,Integer pid, Integer num,
                                       Integer uid, String username,
                                       HttpSession session){
//        UserEntity user=(UserEntity)session.getAttribute("user");
        resp.setHeader("Access-Control-Allow-Origin", "*");
        service.createCart(pid,num,uid,username);
        return JsonResult.getSuccessJR();
    }

    /**
     * order-service访问
     * @param cids
     * @param uid
     * @return
     */
    @PostMapping("/findByCids")
    public List<CartVO> findByCids(HttpServletResponse resp,@PathVariable Integer[] cids, Integer uid) {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        return service.findByCids(cids, uid);
    }

    /**
     * order-service访问
     * @param cids
     */
    @PostMapping("/removeCartListByOrder")
    public void removeCartListByOrder(HttpServletResponse resp,@RequestBody Integer[] cids) {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        service.removeCartList(cids);
    }
}
